﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace server
{
    public class Util
    {
        public static RootObject getRootObject(string result) {

            string codestr = result.Substring(8, 8);
            //string[] codeitems = getstr(codestr, 2);
           // codestr = "";
           // for (int i = 0; i < codeitems.Length; i++)
           // {
               // codestr += codeitems[i];
           // 
           // }
            int num = Convert.ToInt32(codestr);
         

            var root = new RootObject();
            var basesql = "select ln_barn_base_info.ckbh,ln_barn_base_info.ckmc from ln_barn_base_info  where REPLACE(ln_barn_base_info.ckmc,'号仓','')='" + num + "'";
            var ln_barn_base_info = SQLHelper.GetDataSet(basesql).Tables[0];

            var timestr = gettimestr(result);
            var personandtime = SQLHelper.GetDataSet("select * from personandtime where timestr='" + timestr + "' order by id desc").Tables[0];
            if (ln_barn_base_info.Rows.Count > 0) {
                root.ckbh = ln_barn_base_info.Rows[0]["ckbh"].ToString();
                root.ckmc = ln_barn_base_info.Rows[0]["ckmc"].ToString();
            }
         
            if (personandtime.Rows.Count > 0) {
                root.czry = personandtime.Rows[0]["personname"].ToString();
            }
        


            return root;
        }
        public static string gettimestr(string result) {
            string timestr = result.Substring(16, 12);
            string[] items = Util.getstr(timestr, 2);
            timestr = "";
         
            for (int i = 0; i < items.Length; i++)
            {
                int a = Convert.ToInt32(items[i], 16);
                string str = "";
    
                if (a < 10) {
                    str = "0" + a;
                }
                else {
                    str = "" + a;
                }
                timestr += str;
               // timestr += Convert.ToInt32(items[i]).ToString("X2");
            }

            return timestr;
        }
        public static String[] getstr(string strs, int len)
        {
            double i = strs.Length;
            string[] myarray = new string[int.Parse(Math.Ceiling(i / len).ToString())];
            for (int j = 0; j < myarray.Length; j++)
            {
                len = len <= strs.Length ? len : strs.Length;
                myarray[j] = strs.Substring(0, len);
                strs = strs.Substring(len, strs.Length - len);
            }
            return myarray;
        }
    }
}
